package com.task.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.util.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.task.mapper.TaskMapper;
import com.task.pojo.Task;
import com.task.service.TaskService;
import com.task.vo.TaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2024年12月23日
 */
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService {

    @Autowired
    private TaskMapper taskMapper;


    @Override
    public PageResult<Task> pageFind(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize, true);
        Page<Task> page = (Page<Task>) taskMapper.selectList(null);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public PageResult<Task> queryPageFind(TaskVo taskVo, Integer pageNum, Integer pageSize) {
        //设置分页
        PageHelper.startPage(pageNum, pageSize, true);
        //校验条件
        QueryWrapper<Task> taskQueryWrapper = new QueryWrapper<>();

        if (!ObjectUtils.isEmpty(taskVo)) {
            //1.1 发起日期
            if (!ObjectUtils.isEmpty(taskVo.getStartTime())) {
                taskQueryWrapper.gt("lauchtime", taskVo.getStartTime());
            }
            if (!ObjectUtils.isEmpty(taskVo.getEndTime())) {
                taskQueryWrapper.lt("lauchtime", taskVo.getEndTime());
            }
            //1.2 任务类型
            if (!ObjectUtils.isEmpty(taskVo.getTaskid())) {
                taskQueryWrapper.eq("taskid", taskVo.getTaskid());
            }
            //1.3 审核状态
            if (!ObjectUtils.isEmpty(taskVo.getState())) {
                taskQueryWrapper.eq("state", taskVo.getState());
            }
        }

        Page<Task> page = (Page<Task>) taskMapper.selectList(taskQueryWrapper);

        return new PageResult(page.getTotal(), page.getResult());
    }
}
